package tournamentData;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import dataBase.DbHolder;

public class ScoresComparator implements Comparator < Integer > {
	private static ScoresComparator instance = null;

	private ScoresComparator() {

	}

	public static ScoresComparator getInstance() {
		if (instance == null) {
			instance = new ScoresComparator();
		}
		return instance;
	}

	/**
	 * sorts a given list of playerIDs by their score and returns the sorted
	 * list
	 */

	public void sortList(ArrayList < Integer > toSort) throws IOException {
		Collections.sort(toSort, this);
	}

	/**
	 * @return returns -1 if score1 > score2, 0 if they are equal and 1 if
	 *         score1 < score2
	 */
	@Override
	public int compare(Integer player1, Integer player2) {
		try {
			String tournamentType = TournamentData.getInstance().getGeneralTournamentInformation().getTournamentType();
			String bowCathegory = DbHolder.getInstance().getArcherData().getById(player1).getBowCathegory();
			String ageClass = DbHolder.getInstance().getArcherData().getById(player1).getAgeClass();
			String gender = DbHolder.getInstance().getArcherData().getById(player1).getGender();
			int numberOfRounds;

			numberOfRounds = DbHolder.getInstance().getArcherData().getNumberOfRounds(tournamentType, bowCathegory, ageClass, gender);

			GlobalScore score1 = TournamentData.getInstance().getScore(player1);
			GlobalScore score2 = TournamentData.getInstance().getScore(player2);

			// if player 1 has more points
			if (score1.getTotalPoints() > score2.getTotalPoints()) {
				return -1;
			}
			// if player 1 has same points but more arrows in the center
			if ((score1.getTotalPoints() == score2.getTotalPoints()) && (score1.getTotalXS() > score2.getTotalXS())) {
				return -1;
			}
			// if player 1 has same points and same amount of arrows in the
			// center
			// but more tens than opponent
			if ((score1.getTotalPoints() == score2.getTotalPoints()) && (score1.getTotalXS() == score2.getTotalXS()) && (score1.getTotalTens() > score2.getTotalTens())) {
				return -1;
			}
			// if player 1 has same points, same amount of arrows in the center
			// and
			// same amount of tens but more nines than opponent
			if ((score1.getTotalPoints() == score2.getTotalPoints()) && (score1.getTotalXS() == score2.getTotalXS()) && (score1.getTotalTens() == score2.getTotalTens())
					&& (score1.getTotalNines() > score2.getTotalNines())) {
				return -1;
			}

			// if players have equal values in all cathegories
			if ((score1.getTotalPoints() == score2.getTotalPoints()) && (score1.getTotalXS() == score2.getTotalXS()) && (score1.getTotalTens() == score2.getTotalTens())
					&& (score1.getTotalNines() == score2.getTotalNines())) {
				return 0;
			}

			// if none of the above rules applied player2 is stirctly better
			return 1;
		} catch (SQLException e) {
			return 0;
		}
	}
}
